<template>
  <div class="register-page">
    <div class="register-container">
      <h2 class="title">重设密码</h2>
      <el-form :model="registerForm" :hide-required-asterisk="true" label-position="top" :rules="registerRules"
        ref="registerForm" label-width="0">
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="registerForm.phone" placeholder="请输入手机号" clearable class="custom-input" />
        </el-form-item>
        <el-form-item prop="captcha" label="图形验证码">
          <div class="captcha-container">
            <el-input v-model="registerForm.captcha" placeholder="请输入图形验证码" clearable class="custom-input" />
            <img :src="captchaImage" alt="验证码" @click="refreshCaptcha" class="captcha-image" />
          </div>
        </el-form-item>
        <el-form-item label="手机验证码" prop="smsCode">
          <div class="sms-container">
            <el-input v-model="registerForm.smsCode" placeholder="请输入手机验证码" clearable class="custom-input" />
            <el-button type="text" @click="sendSmsCode" :disabled="smsCountdown > 0" class="sms-button">
              {{ smsCountdown > 0 ? `${smsCountdown}s后重新获取` : '获取手机验证码' }}
            </el-button>
          </div>
        </el-form-item>
        <el-form-item prop="password" label="设置密码">
          <el-input v-model="registerForm.password" placeholder="请输入密码" show-password clearable class="custom-input" />
        </el-form-item>
        <el-form-item prop="confirmPassword" label="再次输入密码">
          <el-input v-model="registerForm.confirmPassword" placeholder="请再次输入密码" show-password clearable
            class="custom-input" />
        </el-form-item>
        <el-button @click="submitForm" class="dlBtn" size="medium" type="primary" style="width:100%;">
          确定
        </el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getCodeImg, getSmsCode, resetPassword } from "@/api/login";

export default {
  name: "forget",
  data () {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.password !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      codeUrl: "",
      registerForm: {
        phone: '',
        captcha: '',
        smsCode: '',
        password: '',
        confirmPassword: ''
      },
      registerRules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' },
        ],
        smsCode: [{ required: true, message: '请输入手机验证码', trigger: 'blur' }],
        captcha: [{ required: true, message: '请输入图形验证码', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        confirmPassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          {
            validator: (rule, value) =>
              value === this.registerForm.password
                ? Promise.resolve()
                : Promise.reject(new Error('两次输入密码不一致')),
            trigger: 'blur',
          },
        ],
      },
      loading: false,
      captchaImage: '',
      captchaId: '',
      smsCountdown: 0
    };
  },
  created () {
    this.refreshCaptcha();
  },
  methods: {
    refreshCaptcha () {
      getCodeImg().then(res => {
        this.captchaImage = "data:image/gif;base64," + res.captcha_image;
        this.captchaId = res.captcha_id;
      });
    },
    async sendSmsCode () {
      let that = this;
      if (!this.registerForm.phone || !this.registerForm.captcha) {
        this.$modal.msgError('请先填写手机号和图形验证码');
        return false
      }
      let smsRes = await getSmsCode({
        phone: that.registerForm.phone,
        captcha_id: that.captchaId,
        captcha_value: that.registerForm.captcha,
      });
      if (smsRes.code === 0) {
        this.$modal.msgSuccess("验证码发送成功请注意查收！");

        that.smsCountdown = 60;
        var interval = setInterval(() => {
          if (that.smsCountdown > 0) {
            that.smsCountdown = that.smsCountdown - 1;
          } else {
            clearInterval(interval);
          }
        }, 1000);
      } else {
        this.$modal.msgError(smsRes.message);
        this.refreshCaptcha()
      }

    },
    submitForm () {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true;
          resetPassword({
            phone: this.registerForm.phone,
            new_password: this.registerForm.password,
            sms_code: this.registerForm.smsCode,
          }).then(res => {
            const username = this.registerForm.phone;
            this.$alert("<font color='red'>恭喜你，您的账号 " + username + " 重设成功！</font>", '系统提示', {
              dangerouslyUseHTMLString: true,
              type: 'success'
            }).then(() => {
              this.$router.push("/login");
            }).catch(() => { });
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.refreshCaptcha();
            }
          })
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.register-page {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(180deg, #ffffff 0%, #f3f7ff 100%);
}

.register-container {
  width: 500px;
  border-radius: 12px;

  ::v-deep .el-form-item__label {
    font-size: 14px;
    color: #00011B;
    line-height: 16px;
    margin-bottom: 4px;
    font-weight: normal;
    padding: 0;
  }

  ::v-deep .el-form-item {
    margin-bottom: 24px;
  }

  ::v-deep .el-input {
    height: 48px !important;
    line-height: 48px !important;

    input {
      height: 48px;
      line-height: 48px !important;
      font-size: 16px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }

}

.title {
  font-size: 24px;
  font-weight: bold;
  color: #333;
  margin-bottom: 30px;
  text-align: center;
}

.custom-input {
  width: 100%;
  height: 45px;
  font-size: 16px;
  box-sizing: border-box;
  /* 防止样式溢出 */
}

.captcha-container {
  display: flex;
  align-items: center;
}

.captcha-image {
  width: 148px;
  height: 48px;
  border-radius: 4px;
  cursor: pointer;
  margin-left: 12px;
  border: 1px solid #F1F1F1;
}

.sms-container {
  display: flex;
  align-items: center;
}

.sms-button {
  position: absolute;
  color: rgba(22, 93, 235, 1);
  font-size: 16px;
  line-height: 22px;
  padding: 0;
  border-radius: 0;
  right: 16px;
  border-bottom: 1px solid #165deb;
}

.custom-button {
  width: 100%;
  margin-top: 20px;
  height: 45px;
  font-size: 16px;
}

.footer {
  margin-top: 20px;
  text-align: center;
  font-size: 14px;
  color: #666;
}

.title {
  margin: 0px auto 48px auto;
  text-align: center;
  font-size: 32px;
  color: #000000;
  line-height: 38px;

  span {
    color: #FF7300;
    font-weight: bold;
    margin-left: 8px;
  }
}

.dlBtn {
  height: 54px;
  line-height: 54px;
  font-size: 18px;
  color: #FFFFFF;
  font-weight: bold;
  padding: 0;
  margin-top: 40px;
}

.zc {
  font-size: 14px;
  color: #00011B;
  line-height: 16px;
  margin-top: 16px;
}
</style>
